﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DAL
{
    public class TaiKhoanDAL
    {
        PhongMachTuDBDataContext db = new PhongMachTuDBDataContext();
        public List<TAIKHOAN> LayDSTaiKhoan()
        {
            var q = from s in db.TAIKHOANs
                    select s;
            return q.ToList();
        }

        public List<TAIKHOAN> LayDSTaiKhoan(string tenDN)
        {
            var q = from s in db.TAIKHOANs
                    where s.TenDangNhap==tenDN
                    select s;
            return q.ToList();
        }

        public bool ThemTaiKhoan(TAIKHOAN s)
        {

            try
            {
                TAIKHOAN r = new TAIKHOAN();
                r.HoTen = s.HoTen;
                r.ChucVu = s.ChucVu;
                r.SDT = s.SDT;
                r.MatKhau = s.MatKhau;
                r.TenDangNhap = s.TenDangNhap;
                db.TAIKHOANs.InsertOnSubmit(r);
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool SuaTaiKhoan(TAIKHOAN s)
        {

            try
            {
                TAIKHOAN r = db.TAIKHOANs.Single(id => id.MaTaiKhoan == s.MaTaiKhoan);
                r.HoTen = s.HoTen;
                r.ChucVu = s.ChucVu;
                r.SDT = s.SDT;
                r.MatKhau = s.MatKhau;
                r.TenDangNhap = s.TenDangNhap;
          
                db.SubmitChanges();
                return true;

            }
            catch (Exception)
            {

                return false;
            }
        }
        public bool xoaTaiKhoan(int TaiKhoanID)
        {
            try
            {
                var delete = from d in db.TAIKHOANs
                             where d.MaTaiKhoan == TaiKhoanID
                             select d;
                if (delete.Count() > 0)
                {
                    db.TAIKHOANs.DeleteOnSubmit(delete.FirstOrDefault());
                    db.SubmitChanges();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
        public bool kiemTraDN(TAIKHOAN tk)
        {
            var check = from c in db.TAIKHOANs
                        where c.TenDangNhap == tk.TenDangNhap && c.MatKhau == tk.MatKhau
                        select c;
            if (check.Count() > 0)
            {
                return true;
            }
            return false;
        }

    }
}
